<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * This Controller provides all Management Operations
 *
 * @author febusch
 */
class Data_management extends TMT_Controller {
    
    function __construct() 
    {
        parent::__construct();
    }
    
    public function index()
    {
        redirect(__CLASS__.'/teams');
    }
    
    public function teams()
    {
        
        $this->headline = 'Übersicht über alle Teams';
        //$this->view_data['teams'] = $this->teamdata->get_all();
        $this->view_data['teams'] = Model\Team::all();//$this->employee->team();
    }
    
    public function budget($TeamID = NULL)
    {
        $err = $this->input->get('err');
        if(!empty($err) AND $err == 'nn')
        {
            $this->view_data['error'] = 'Achtung: Wert ist nicht numerisch!';
            header('Refresh: 2; url='.base_url().'index.php/data_management/budget/'.$TeamID);
        }

        // Wenn leer
        if(empty($TeamID))
        {
            $this->view_data['error'] = 'Achtung: Kein Team ausgewählt!';
			return;
        }
		
		$team = Model\Team::find($TeamID);
        // Wenn bereits Budget eingetragen wurde
        if($team->budget != NULL)
        {
            $this->view_data['error'] = 'Achtung: Diesem Team ist bereits ein Budget zugewiesen!';
        }
        
        // Wenn alles glatt läuft
        else
        {
            $teamname = $team->name;
            
            $this->headline = 'Budget für Team "'.$teamname. '" setzen';
            
            $this->view_data['action'] = __CLASS__.'/set_budget/'.$TeamID;
        }
 
    }
    
    public function set_budget($TeamID = NULL)
    {
		$value = str_replace('.','',$this->input->post('budget'));
        $value = str_replace(',','.',$value);
        if(is_numeric($value))
        {
            if(!empty($TeamID) AND !empty($value))
            {
				$team = Model\Team::find($TeamID);
				if($team)
				{
					$team->budget = $value;
					$team->save();
				}
				else
				{
					return FALSE;
				}
            }
            redirect(__CLASS__);
        } else { redirect(__CLASS__.'/budget/'.$TeamID.'?err=nn'); }
    }
    
    public function create_measure()
    {
        $this->headline = 'Eine neue Maßnahme erstellen';
        
    }
    
    public function team($TeamID = NULL)
    {
        $team = Model\Team::find($TeamID);
        $this->headline = 'Teamdetails: '. $team->name;
        
        $members = Model\Team::find($TeamID)->employee();
        $teamleader = Model\Team::find($TeamID)->teamleader();


        $this->view_data['team'] = $team;
        $this->view_data['members'] = $members;
        $this->view_data['teamleader'] = $teamleader;
    }
    
}
